/*
 * Copyright 2010-2011 WebEqua Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.mathcloud.math.core.matlab.operator.algebra;

//import classes slf4j
import org.mathcloud.math.core.matlab.factory.MatlabQuantityObjectFactory;
import org.mathcloud.math.core.matlab.operator.MatlabNaryOperator;
import org.mathcloud.math.core.matlab.quantity.MatlabQuantityObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import classes org.webequa.core.common
import org.webequa.core.common.operator.algebra.Lcm;
import org.webequa.core.common.quantity.QuantityObject;
import org.webequa.core.common.signature.QuantityObjectSignature;
//import classes org.webequa.core.matlab
//import classes com.mathworks.toolbox.javabuilder
import com.mathworks.toolbox.javabuilder.MWArray;
import com.mathworks.toolbox.javabuilder.MWCharArray;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

public class MatlabLcm extends MatlabNaryOperator implements Lcm
{
	public MatlabLcm()
	{
		super();
		signature.setMMLContentTag("lcm");
	}	
	
	@Override	
	public QuantityObject calculate(QuantityObject operand1, QuantityObject operand2)
	{
		QuantityObject out		= null;
		
		QuantityObjectSignature resSign = getResultSignature(operand1, operand2);
		if (resSign!=null)
	    {
	      
	      MWCharArray nameFunction 	= null;
		  Object[] result 			= null;
		  String functionName       = "lcm";
			
			try
			{
				log.debug("Work with '{}' funtion Matlab",functionName);
				MatlabQuantityObject tOp1 = MatlabQuantityObjectFactory.getInstance(operand1);
				MatlabQuantityObject tOp2 = MatlabQuantityObjectFactory.getInstance(operand2);
				nameFunction = new MWCharArray(functionName);
				result = mtlbEngine.Matlab4WebEqua(1,nameFunction, tOp1.getMatlabObject(), tOp2.getMatlabObject());
				log.debug("getResult from '{}' function",functionName);
				out = MatlabQuantityObjectFactory.getInstance((MWNumericArray)result[0]);
			} catch (Exception e)
			{
				log.error("Exception:",e);
			} finally
		    {
		         /* Free native resources */
		         MWArray.disposeArray(nameFunction);
		         MWArray.disposeArray(result);
		    }
	    } 
		else {
			log.warn("ResultSignature isn't defined");
		}	
		return out;
	}
	
	private static final Logger log = LoggerFactory.getLogger(MatlabLcm.class);	
}
